//
//  MultiplayerScoreView.m
//  Quiz
//
//  Created by AnhLH-Oriolus on 31/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MultiplayerScoreView.h"


@implementation MultiplayerScoreView

@synthesize arrScore, arrName;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		UILabel *lblOwnScore = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 45)]; 
		[lblOwnScore setBackgroundColor:[UIColor clearColor]];
		lblOwnScore.textAlignment = UITextAlignmentCenter;
		lblOwnScore.textColor = [UIColor whiteColor];
		lblOwnScore.font = [UIFont fontWithName:@"Arial" size:18];
		lblOwnScore.text = @"Multiplayer's Score";
		[self addSubview:lblOwnScore];
		[lblOwnScore release];
		
		_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, 300, 180) style:UITableViewStylePlain];
		[_tableView setBackgroundColor:[UIColor clearColor]];
		_tableView.rowHeight = 30;
		_tableView.dataSource = self;
		_tableView.delegate = self;
		[self addSubview:_tableView];
		//[_tableView release];
		
		arrName = [[NSMutableArray alloc] init];
		arrScore = [[NSMutableArray alloc] init];
		
		[arrName addObject:@"Anh"];
		[arrScore addObject:[NSNumber numberWithInt:1000]];
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


-(void)setDataWithPlayerName:(NSMutableArray *)arrNames withArrScore:(NSMutableArray *)arrScores{
	//if(arrName){
	//		[arrName removeAllObjects];
	//		arrName = nil;
	//	}
	//	if(arrScore){
	//		[arrScore removeAllObjects];
	//		arrScore = nil;
	//	}
	
	arrScore = [arrScores retain];
	arrName = [arrNames retain];
	
	[_tableView reloadData];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	//if([arrName count] > 100)
	//	return 100;
	
	return [arrScore count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	ScoreCellView* cell = nil;
	
	cell = (ScoreCellView *)[tableView dequeueReusableCellWithIdentifier:@"mulScore"];
	if(!cell){
		cell = [[[ScoreCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mulScore" withMode:0] autorelease];
	}
	
	NSString *nameFormat = [NSString stringWithFormat:@"%d. %@", indexPath.row+1, [arrName objectAtIndex:indexPath.row]];
	[cell setPlayerName:nameFormat withScore:[[arrScore objectAtIndex:indexPath.row] intValue]];
	
	return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)dealloc {
	if(arrName){
		[arrName removeAllObjects];
		arrName = nil;
	}
	if(arrScore){
		[arrScore removeAllObjects];
		arrScore = nil;
	}
	
    [super dealloc];
}


@end
